<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\TemplateMessage;
use App\Traits\Controllers\DestroyTraits;
use App\Traits\Controllers\ShowTraits;
use App\Transformers\Template\TemplateMessageTransformer;
use Illuminate\Http\Request;

class WechatTemplateMessagesController extends Controller
{

    use DestroyTraits;
    use ShowTraits;

    public $model = TemplateMessage::class;

    public $transformer = TemplateMessageTransformer::class;

    /**
     * 微信模板消息列表
     */
    public function index(Request $request)
    {
        $this->formatIndexQuery($request);

        $data = $this->getQuery()
            ->where('type','temp_message')
            ->pimp()
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }

    /**
     * 微信模板消息添加
     */
    public function store(Request $request)
    {
        $this->validateData($request);

        $request->offsetSet('system_admin_id', $this->admin_user('id'));
        $request->offsetSet('type', 'temp_message');

        $model = $this->getQuery()->fill($request->all());
        $model->save();

        return $this->response->created();
    }

    /**
     * 微信模板消息修改
     */
    public function update($id, Request $request)
    {
        //验证用户凭证信息
        $this->validateData($request);

        $data = $this->getQuery()->findOrFail($id);

        if ($this->policy) {
            $this->authorize('update', $data);
        }

        $request->offsetSet('template_key',$data->template_key);
        $request->offsetSet('name',$data->name);
        $request->offsetSet('content',$data->content);

        $data->fill($request->all());
        $data->save();

        return $this->response->noContent();
    }

    /**
     * 状态修改
     */
    public function status($id,Request $request)
    {

        //验证用户凭证信息
        $this->validateData($request);

        if (empty($id)) {
            return $this->response->errorBadRequest();
        }

        $this->getQuery()->where('id',$id)->update([
            'status' => $request->input('status')
        ]);

        return $this->response->noContent();
    }

    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [
                    'template_id' => 'bail|required',
                    'template_key' => 'bail|required',
                    'name' => 'bail|required',
                    'content' => 'bail|required',
                ];
                break;
            case 'PATCH':
                return [
                    'template_id' => 'bail|required',
                ];
                break;
            case 'PUT':
                return [
                    'status' => 'bail|required',
                ];
                break;
        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
            'template_id.required' => '模板id 不能为空',
            'template_key.required' => '模板编号 不能为空',
            'name.required' => '模板名 不能为空',
            'content.required' => '回复内容 不能为空',
        ];
    }
}
